package com.taptech.doufu.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.View
import android.widget.Toast
import com.taptech.doufu.R
import com.taptech.doufu.constant.EventKey
import com.taptech.doufu.constant.SPKey
import com.taptech.doufu.event.BaseEventBusBean
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity
import com.taptech.doufu.util.TimeDateFunction
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences
import kotlinx.android.synthetic.main.activity_youth_mode_pwd.*
import org.greenrobot.eventbus.EventBus

class YouthModelTipActivity:DiaobaoBaseActivity() {

    private var inputPwd = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_youth_mode_pwd)
        etPwd.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                inputPwd = s.toString()
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                when(s?.length){
                    0->{
                        tvPwd1.text = ""
                        tvPwd2.text = ""
                        tvPwd3.text = ""
                        tvPwd4.text = ""
                    }
                    1->{
                        tvPwd1.text = "*"
                        tvPwd2.text = ""
                        tvPwd3.text = ""
                        tvPwd4.text = ""
                    }
                    2->{
                        tvPwd1.text = "*"
                        tvPwd2.text = "*"
                        tvPwd3.text = ""
                        tvPwd4.text = ""
                    }
                    3->{
                        tvPwd1.text = "*"
                        tvPwd2.text = "*"
                        tvPwd3.text = "*"
                        tvPwd4.text = ""
                    }
                    4->{
                        tvPwd1.text = "*"
                        tvPwd2.text = "*"
                        tvPwd3.text = "*"
                        tvPwd4.text = "*"
                    }
                }
            }

        })


        layTop.visibility = View.INVISIBLE
        tvTitle.text = "防沉迷模式"
        tvDesc.text = "为了保障充足的休息时间，您在每日22时至次日6时期间无法使用豆花，或由监护人输入密码后继续使用。"
        btnSure.text = "确认"
        tvFindPwd.visibility = View.VISIBLE
        tvFindPwd.setOnClickListener {
            YouthModelFindPwdActivity.startActivity(thisActivity)
        }
        btnSure.setOnClickListener {
            if (inputPwd.length == 4){
                val pwd = DiaoBaoSharedPreferences.getSharedPreferencesValueToString(SPKey.YOUTH_MODEL_PWD,thisActivity,"")
                if (inputPwd == pwd) {
                    finish()
                }else{
                    etPwd.setText("")
                    inputPwd = ""
                    Toast.makeText(thisActivity,"密码不正确，请重新输入",Toast.LENGTH_LONG).show()
                }
            }else{
                Toast.makeText(thisActivity,"请输入完整密码",Toast.LENGTH_LONG).show()
            }

        }

    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //拦截返回
            return false
        }
        return super.onKeyDown(keyCode, event)
    }

    companion object{

        fun startActivity(context: Context) {
            val intent = Intent()
            intent.setClass(context,YouthModelTipActivity::class.java)
            context.startActivity(intent)
        }
    }
}